package com.it.admin.event.subscriber;

import com.it.admin.event.ReadArticleEvent;
import com.it.common.mapper.ArticleMapper;
import com.it.common.mapper.StatisticsArticlePVMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

@Component
@Slf4j
public class ReadArticleSubscriber implements ApplicationListener<ReadArticleEvent> {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private StatisticsArticlePVMapper statisticsArticlePVMapper;

    @Override
    @Async("threadPoolTaskExecutor")
    public void onApplicationEvent(ReadArticleEvent event) {
        Long articleId = event.getArticleId();
        // 获取当前线程名称
        String threadName = Thread.currentThread().getName();

        log.info("==> threadName: {}", threadName);
        log.info("==> 文章阅读事件消费成功，articleId: {}", articleId);
        articleMapper.increaseReadNum(articleId);
        log.info("==> 文章阅读量 +1 操作成功，articleId: {}", articleId);
        // 当日文章 PV 访问量 +1
        LocalDate currentDate = LocalDate.now();
        statisticsArticlePVMapper.increasePVCount(currentDate);
        log.info("==> 当日文章 PV 访问量 +1 操作成功，date: {}", currentDate);
    }
}
